﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TM.QWeibo.Client.Helper;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;

namespace TM.QWeibo.Client.UserControls
{
    public partial class TweetDetail : UserControl
    {
        #region 私有变量

        private SDK.Objects.Tweet _tweet;

        #endregion

        #region 公共变量
        /// <summary>
        /// 选择微博
        /// </summary>
        public Action<string> SelectedSource { get; set; }

        /// <summary>
        /// 查看视频
        /// </summary>
        public Action<string> ViewVideo{ get; set; }

        /// <summary>
        /// 查看图片
        /// </summary>
        public Action<string> ViewImage { get; set; }

        #endregion

        #region 控件事件

        /// <summary>
        /// 加载后回调
        /// </summary>
        private Action<SDK.Objects.Tweet> afterLoadedCallback { get; set; }

        #endregion

        public TweetDetail()
        {
            InitializeComponent();
            mainImage.MaxWidth = App.Current.RootVisual.RenderSize.Width-50;
        }
        /// <summary>
        /// 加载Tweet的详细信息
        /// </summary>
        /// <param name="tweetId"></param>
        public void LoadTweet(string tweetId, Action beforeLoading = null, Action<SDK.Objects.Tweet> afterCallback = null)
        {
            //mainImage.Source = new BitmapImage(new Uri("/Resource/Images/imageHolder.png", UriKind.Relative));
            if (beforeLoading!=null) {
                beforeLoading();
            }
            if (afterCallback != null) {
                afterLoadedCallback = afterCallback;
            }
            Dal.Tweet.Instance.Get(tweetId, GetTweetCallback);
        }

        /// <summary>
        /// 加载回调
        /// </summary>
        /// <param name="rs"></param>
        private void GetTweetCallback(SDK.Objects.Tweet rs)
        {
            Dispatcher.BeginInvoke(() => {
                _tweet = rs;
                TweetDetailLayoutRoot.DataContext = rs;
                //mainImage.Source = new BitmapImage(new Uri(rs.Image + "/460", UriKind.Absolute));
                rtRichTweetBox.SetContent(rs.Origtext);

                if (afterLoadedCallback != null)
                {
                    afterLoadedCallback(rs);
                }
            });
        }

        private void Grid_Tap(object sender, GestureEventArgs e)
        {
            if (SelectedSource != null && _tweet!=null) {
                SelectedSource(_tweet.Source.Id);
            }
        }

        /// <summary>
        /// 点击视频图片打开视频
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VideoImage_Tap(object sender, GestureEventArgs e)
        {
            if (ViewVideo!=null && _tweet != null && _tweet.Video!=null) {
                ViewVideo(_tweet.Video.RealUrl);
            }
        }

        /// <summary>
        /// 点击图片打开图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TweetImage_Tap(object sender, GestureEventArgs e)
        {
            if (ViewImage != null && _tweet != null)
            {
                ViewImage(_tweet.Image);
            }
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            Helper.ImageHelper.IsGif(_tweet.Image, (b) =>
            {
                if (b)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        mainImage.Source = new BitmapImage(new Uri(_tweet.Image + "/160", UriKind.Absolute));
                        var imageBrush = new ImageBrush
                        {
                            AlignmentX = System.Windows.Media.AlignmentX.Right,
                            AlignmentY = System.Windows.Media.AlignmentY.Bottom,
                            Stretch = System.Windows.Media.Stretch.UniformToFill
                        };
                        imageBrush.ImageSource = new BitmapImage(new Uri("/Resource/Images/GifMask.png", UriKind.Relative));
                        mainImage.OpacityMask = imageBrush;
                    });
                }
            });
        }
    }
}
